package ywpt.web.action.config;

import java.util.ArrayList;
import java.util.List;

import org.ecside.table.limit.Limit;
import org.ecside.util.RequestUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;

import ywpt.core.exception.UniqueConstraintException;
import ywpt.core.web.BaseAction;
import ywpt.model.CostType;
import ywpt.service.config.CostTypeService;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class CostTypeAction extends BaseAction implements
ModelDriven<CostType>, Preparable{

	/**
	 * 费用类型Action
	 */
	private static final long serialVersionUID = -5598340489774230192L;
	private static final int DEFAULT_PAGE_SIZE = 20;
	private Long id;
	
	private CostType costType;
	private List<CostType> costTypeList = new ArrayList<CostType>();
	private CostTypeService costTypeService;
	
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public CostType getCostType() {
		return costType;
	}

	public void setCostType(CostType costType) {
		this.costType = costType;
	}

	public List<CostType> getCostTypeList() {
		return costTypeList;
	}

	public void setCostTypeList(List<CostType> costTypeList) {
		this.costTypeList = costTypeList;
	}

	public CostTypeService getCostTypeService() {
		return costTypeService;
	}

	public void setCostTypeService(CostTypeService costTypeService) {
		this.costTypeService = costTypeService;
	}

	@Override
	public CostType getModel() {
		return this.costType;
	}

	@Override
	public void prepare() throws Exception {
		if( id != null ){
			this.costType = costTypeService.getCostTypeById(id);
		}else{
			this.costType = new CostType();
		}
	}
	// **************************数据交互方法*******************************
	
	public String list(){
		DetachedCriteria query=DetachedCriteria.forClass(CostType.class);
		int totalRows=RequestUtils.getTotalRowsFromRequest(getRequest());
		if(totalRows<0)
		{
			totalRows=costTypeService.getCostTypeCountByCriteria(query);
		}
		int[] rowStartEnd=RequestUtils.getRowStartEnd(getRequest(),
				totalRows,DEFAULT_PAGE_SIZE,0);
		Limit limit=RequestUtils.getLimit(getRequest(),"ec",totalRows,
				DEFAULT_PAGE_SIZE);
		int pageSize=limit.getCurrentRowsDisplayed();
		query.addOrder(Order.asc("code"));
		query.setProjection(null);
		costTypeList.addAll(costTypeService.getCostTypes(query,pageSize,
				rowStartEnd[0]));
		return SUCCESS;
	}
	
	public String input(){	
		String code = this.costTypeService.CreateTypeCode();
		costType.setCode(code);
		return INPUT;
	} 

	public String edit(){
		this.costType = costTypeService.getCostTypeById(id);
		return INPUT;
	}
	/**
	 * 保存
	 * 
	 * @return
	 */
	public String save() {
		try {
			if (id == null) {
				costTypeService.saveCostType(costType);
			} else {
				costTypeService.updateCostType(costType);
			}
			this.addActionMessage("类型信息已保存!");
			return SUCCESS;
		} catch (UniqueConstraintException e) {
				this.addActionError("保存错误:" + e.getMessage());
			return INPUT;
		}
	}
	
	 public String delete() throws Exception {
		 	if (id != null) {
//				DetachedCriteria congtractQuery = DetachedCriteria.forClass(ClientType.class);
//				congtractQuery.add(Restrictions.eq("type", clientTypeService.getClientTypeById(id)));
//				int totalRows = clientService.getClientInfoCountByCriteria(congtractQuery);
//				if (totalRows > 0) {
//					this.addActionMessage("该种类已经发生过业务逻辑，不能删除！");
//					return INPUT;
//				}else{
					costTypeService.removeCostType(id);
					return SUCCESS;					
			//	}

			}
			this.addActionMessage("编号为空，请选择具体的记录！");
			return INPUT;
	}
		public String select() 
		{
			costTypeList.addAll(costTypeService.getCostTypes());
			return SUCCESS;
		}
}
